﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SUIE;

namespace SUIE
{
    [ToolboxItemAttribute(false)]
    public class Tabber : WebPart
    {
        // A Div to display the Content of the Tab.
        private HtmlGenericControl _divTabContent;

        protected override void CreateChildControls()
        {
            this.Hidden = false;
            this.ChromeType = PartChromeType.Default;
            this.CssClass = "SUIETabber SUIETabber-" + this.ID;

            this._divTabContent = new HtmlGenericControl("div");
            this._divTabContent.ID = "SUIETabContent";
            this._divTabContent.InnerHtml = "<a href=\"javascript:ShowToolPane2Wrapper('Edit',this,'" + this.ID + "')\">Edit</a> this Web Part to configure tabs.";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIETabberEnabled'>" + this.TabsEnabled.ToString() + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIETabberOrientation'>" + this.TabOrientation + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIETabberTheme'>" + this.TabTheme + "</div>";
            this._divTabContent.InnerHtml += "<script type=\"text/javascript\">$(document).ready(function () { $('.SUIETabber-" + this.ID + "').SUIETabber(); }); </script>";

            this.Controls.Add(_divTabContent);
        }
        protected override void OnPreRender(EventArgs e)
        {
            // add css            
            AddCss("SUIETabberjQueryUI", "/_layouts/SUIE/css/jquery-ui/smoothness/jquery-ui-1.8.16.custom.css");
            AddCss("SUIETabberTheme-" + this.ID, "/_layouts/SUIE/css/jquery-ui/" + this.TabTheme + "/jquery-ui-1.8.16.custom.css");
            AddCss("SUIETabberBaseStyles", "/_layouts/SUIE/css/Tabber.css");

            const string jquery = "jquery-1.6.2.min.js";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), jquery))
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), jquery, " <script type=\"text/javascript\" src=\"/_layouts/SUIE/scripts/jquery-1.6.2.min.js\" ></script>");
            }
            const string jqueryUI = "jquery-ui-1.8.16.custom.min.js";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), jqueryUI))
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), jqueryUI, " <script type=\"text/javascript\" src=\"/_layouts/SUIE/scripts/jquery-ui-1.8.16.custom.min.js\" ></script>");
            const string Tabber = "Tabber.js";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), Tabber))
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Tabber, " <script type=\"text/javascript\" src=\"/_layouts/SUIE/scripts/Tabber.js\" ></script>");
            base.OnPreRender(e);
        }

        private void AddCss(string id, string href)
        {
            HtmlHead head = (HtmlHead)Page.Header;
            HtmlLink link = new HtmlLink();
            link.Attributes.Add("id", id);
            link.Attributes.Add("href", href);
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            bool styleExists = false;
            foreach (Control c in head.Controls)
            {
                if (c is HtmlLink)
                {
                    HtmlLink csslink = c as HtmlLink;
                    if (csslink.Href == href)
                    {
                        styleExists = true;
                        break;
                    }
                }
            }
            if (!styleExists)
                head.Controls.Add(link);
        }

        public void SaveChanges()
        {
            // This method sets a flag indicating that the personalization data has changed.
            // This will allow the changes to the Web Part properties from outside the Web Part class.
            this.SetPersonalizationDirty();
        }

        // properties       
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue(true)]
        public Boolean TabsEnabled
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue(TabOrientationEnum.horizontal)]
        public TabOrientationEnum TabOrientation
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [DefaultValue("ui-lightness")]
        [Personalizable(PersonalizationScope.Shared)]
        public String TabTheme
        {
            get;
            set;
        }

        public override object WebBrowsableObject
        {
            // Return a reference to the Web Part instance.
            get { return this; }
        }

        public override EditorPartCollection CreateEditorParts()
        {
            TabConfigurationEditorPart editorPart = new TabConfigurationEditorPart();

            // The ID of the Editor part should be unique. So prefix it with the ID of the Web Part.
            editorPart.ID = this.ID + "_SUIETabConfigurationEditorPart";

            // Create a collection of Editor Parts and add them to the Editor Part collection.
            List<TabConfigurationEditorPart> editors = new List<TabConfigurationEditorPart> { editorPart };
            return new EditorPartCollection(editors);
        }


    }
}
